package com.java8.MethodRef;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 引用构造方法 类名::new
 */

public class demo_4 {
    //集合里面存储姓名和年龄，要求封装成Student对象并收集到List集合中
    public static void main(String[] args) {
        //1.创建集合对象
        ArrayList<String> list = new ArrayList<>();
        //2.添加数据
        Collections.addAll(list, "张无忌,15", "周芷若,14", "赵敏,13", "张强,20", "张三丰,100", "张翠山,40", "张良,35", "王二麻子,37", "谢广坤,41");

        //String --> Student
        list.stream().map(new Function<String,Student>() {
            @Override
            public Student apply(String s) {
                String[] arr = s.split(",");
                return new Student(arr[0],Integer.parseInt(arr[1]));
            }
        }).collect(Collectors.toList()).forEach(System.out::println);
        System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        //方法引用
        //专门写一个对应的构造方法
//        public Student(String s){
//            String[] arr = s.split(",");
//            this.name=arr[0];
//            this.age=Integer.parseInt(arr[1]);
//        }
        list.stream()
                .map(Student::new) //传String ,返回Student
                .collect(Collectors.toList())
                .forEach(System.out::println);
    }
}

class Student {
    private String name;
    private int age;
    
    public Student() {
    }
    //专门写一个对应的构造方法
    public Student(String s){
        String[] arr = s.split(",");
       this.name=arr[0];
       this.age=Integer.parseInt(arr[1]);
    }
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student(Integer integer) {
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }
}

